import Vue from 'vue';
import VueRouter from 'vue-router';
import Home from "views/Home/Home";
import NotificationIndex from "views/Notification/Index";
import ProfileIndex from 'views/Profile/Index';
import Explore from "views/Explore/Explore";
// import App from "@/App";


Vue.use(VueRouter)

const Profile = () => import('@/views/Profile/Profile');
const Login = () => import('@/views/Login/Login');
const Register = () => import('@/views/Register/Register');
const Register1 = () => import('@/views/Register/Register1');
const Register2 = () => import('@/views/Register/Register2');
const Index = () => import('@/views/Index');
const ForgetPwd = () => import('@/views/ForgetPwd/ForgetPwd');
const ForgetPwd1 = () => import('@/views/ForgetPwd/ForgetPwd1');
const ForgetPwd2 = () => import('@/views/ForgetPwd/ForgetPwd2');
const Help = () => import('@/views/Profile/Help');
const User = () => import('@/views/Profile/User');
const Setting = () => import('@/views/Profile/Setting');
const ECircle = () => import('@/views/Explore/ECircle');
const Hotpoint = () => import('@/views/Explore/Hotpoint');
const Detailed = () => import('@/views/Explore/Detail');
const AllCircle = () => import('@/views/Explore/AllCircle');
const CircleDetailed = () => import('@/views/Explore/CircleDetail');
const Activity = () => import('@/views/Home/Activity');
const Notification = () => import('@/views/Notification/Notification');
const NotificationDetailed = () => import('@/views/Notification/Detailed');
const Admin = () => import('@/views/Profile/Admin');
const Apply = () => import('@/views/Profile/Apply');
const Mine = () => import('@/views/Notification/Mine');

const routes = [
  {path: '', redirect: '/index'},
  {
    path: '/index',
    component: Index,
    children: [
      {path: '', redirect: 'home'},
      {path: 'activity', component: Activity, meta: {isShow: false}},
      {path: 'home', component: Home},
      {
        path: 'notification',
        component: NotificationIndex,
        children: [
          {path: '', redirect: 'index'},
          {path: 'index', component: Notification},
          {path: 'detailed', component: NotificationDetailed, meta: {isShow: false}},
          {path: 'mine', component: Mine, meta: {isShow: false}}
        ]
      },
      {
        path: 'profileIndex',
        component: ProfileIndex,
        children: [
          {path: '', redirect: 'profile'},
          {path: 'profile', component: Profile},
          {path: 'help', component: Help, meta: {isShow: false}},
          {path: 'user', component: User, meta: {isShow: false}},
          {path: 'setting', component: Setting, meta: {isShow: false}},
          {path: 'admin', component: Admin, meta: {isShow: false}},
          {path: 'apply', component: Apply, meta: {isShow: false}},
        ]
      },
      {
        path: 'explore',
        component: Explore,
        children: [
          {path: '', redirect: 'ecircle'},
          {path: 'ecircle', component: ECircle},
          {path: 'hotpoint', component: Hotpoint},
          {path: 'detailed', component: Detailed, meta: {isShow: false}},
          {path: 'allcircle', component: AllCircle, meta: {isShow: false}},
          {path: 'circledetailed', component: CircleDetailed, meta: {isShow: false}}
        ]
      }
    ]
  },
  {path: '/login', component: Login, name: 'login'},
  {
    path: '/register',
    component: Register,
    children: [
      {path: '', redirect: '1'},
      {path: '1', component: Register1},
      {path: '2', component: Register2}
    ]
  },
  {
    path: '/forgetpwd',
    component: ForgetPwd,
    children: [
      {path: '', redirect: '1'},
      {path: '1', component: ForgetPwd1},
      {path: '2', component: ForgetPwd2}
    ]
  }
]

const router = new VueRouter({
  // mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// router.onError((error) => {
//   const pattern = /Loading chunk (\d)+ failed/g;
//   const isChunkLoadFailed = error.message.match(pattern);
//   const targetPath = router.history.pending.fullPath;
//   if (isChunkLoadFailed) {
//     router.replace(targetPath);
//   }
// })

const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

router.beforeEach((to, from, next) => {
  // if (to.path !== '/login' || to.path !== '/register' || to.path !== '/register/1' || to.path !== '/register/2') {
  //   let isLogin = localStorage.getItem('isLogin') === 'true';
  //   if (!isLogin) {
  //     next(false);
  //     _g.reLogin();
  //   }
  // }
  next();
})

export default router
